package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.CommentOfVideo;
import cn.wolfcode.trip.base.domain.Video;
import cn.wolfcode.trip.base.mapper.CommentOfVideoMapper;
import cn.wolfcode.trip.base.mapper.VideoMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IVideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements IVideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private CommentOfVideoMapper commentOfVideoMapper;
    @Override
    public void save(Video video) {
        videoMapper.insert(video);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Video> list = videoMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Video selectById(Long id) {
        Video video = videoMapper.selectByPrimaryKey(id);
        return video;
    }

    @Override
    public void changeState(Video video) {
        if (video.getState() == 1){
            video.setTime(new Date());
        }
        videoMapper.updateByPrimaryKey(video);
    }

    @Override
    public void saveComment(CommentOfVideo comment) {
        comment.setTime(new Date());
        commentOfVideoMapper.insert(comment);
    }

    @Override
    public PageInfo queryComment(CommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<CommentOfVideo> list = commentOfVideoMapper.query(qo);
        return new PageInfo(list);
    }

    @Override
    public void addNum(Long id) {
        videoMapper.addNum(id);
    }
}
